﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class SpecialNewsAddControl : YuanlinUserControl
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedDropDownList provinceDropDownList;
        protected ValuedDropDownList cityDropDownList;
        protected ValuedDropDownList townDropDownList;
        protected ValuedDropDownList smallTownDropDownList;
        protected ValuedEditor descriptionValuedEditor;
        protected HiddenField townHidden;
        protected HiddenField smallTownHidden;
        protected ValuedDropDownList categoryDropDownList;
        protected IButton SaveButton;
        protected IButton BackButton;

        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
            }
            AjaxManager.Register(this, "SpecialNews");
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            SpecialNews specialNews = new SpecialNews();

            specialNews.Title.Value = CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
            specialNews.Body.Value = CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);
            specialNews.Province.Value = Convert.ToInt32(provinceDropDownList.Value);
            specialNews.City.Value = Convert.ToInt32(cityDropDownList.Value);
            specialNews.CreateDate.Value = DateTime.Now;
            specialNews.CategoryId.Value = Globals.ChangeType<int>(categoryDropDownList.Value);
            specialNews.CategoryType.Value = categoryType;
            specialNews.Kind.Value = kind;

            int town = Convert.ToInt32(townDropDownList.Value);
            if (!string.IsNullOrEmpty(townHidden.Value))
            {
                town = Convert.ToInt32(townHidden.Value);
            }
            specialNews.Town.Value = town;

            int smallTown = Convert.ToInt32(smallTownDropDownList.Value);
            if (!string.IsNullOrEmpty(smallTownHidden.Value))
            {
                smallTown = Convert.ToInt32(smallTownHidden.Value);
            }
            specialNews.SmallTown.Value = smallTown;

            BusinessManager.CreateSpecialNews(specialNews);

            PageRedirect();
        }
        private void UpdateAreaData(Area area)
        {
            if (categoryType == (int)CategoryType.HuiZhanNews)
            {
                area.HuamuHuizhanNewsCount.Value++;
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                area.HuamuShichangNewsCount.Value++;
            }
            else if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                area.HuamuZhiXiangNewsCount.Value++;
            }
        }
        private void PageRedirect()
        {
            if (categoryType == (int)CategoryType.HuiZhanNews)
            {
                Page.Response.Redirect(SiteUrls.Instance().GetHuamuHuizhanNewsListUrl());
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                Page.Response.Redirect(SiteUrls.Instance().GetHuamuShichangNewsListUrl());
            }
            else if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                Page.Response.Redirect(SiteUrls.Instance().GetHuamuZhiXiangNewsListUrl());
            }
        }
        private void Bind()
        {
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = 0;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(provinceDropDownList, areas, "EntityId", "Title");
            if (this.provinceDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.provinceDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(cityDropDownList, areas, "EntityId", "Title");
            if (this.cityDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.cityDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(townDropDownList, areas, "EntityId", "Title");
            if (this.townDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.townDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(smallTownDropDownList, areas, "EntityId", "Title");

            InitCategory();
        }
        private void BindDropDownList(ValuedDropDownList ddl, EntityList list, string valueField, string textField)
        {
            ddl.DataSource = list;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();
        }
        private void InitCategory()
        {
            Hashtable categorys = new Hashtable();
            categorys.Add(1, "种植");
            categorys.Add(2, "养殖");
            categorys.Add(3, "资材");
            categoryDropDownList.DataSource = categorys;
            categoryDropDownList.DataTextField = "value";
            categoryDropDownList.DataValueField = "key";
            categoryDropDownList.DataBind();
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = parentCategoryID;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas.Count > 0)
            {
                string areaFormat = "{0}:{1}";
                string areaString = string.Empty;
                foreach (Area area in areas)
                {
                    areaString += ";" + string.Format(areaFormat, area.EntityId.Value.ToString(), area.Title.Value);
                }
                if (areaString.Length > 0)
                {
                    areaString = areaString.Substring(1);
                }
                return areaString;
            }

            return string.Empty;
        }

    }
}